<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of generatepiutang
 *
 * @author miyzan
 */
class generatepiutang extends MY_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model("generatepiutang_model", "datamodel");
    }

    public function get_rows_preview() {        
        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';       
        $result = $this->datamodel->get_rows_preview($thajaran,$thbl,$search, $start, $limit);
        echo $result;
    }
      public function get_rows_preview_nis() {        
        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';       
        $result = $this->datamodel->get_rows_preview_nis($nis,$thajaran,$thbl,$search, $start, $limit);
        echo $result;
    }
    
    public function get_rows_resgen() {
        
        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';       
        $result = $this->datamodel->get_rows_resgen($thajaran,$thbl,$search, $start, $limit);
        echo $result;
    }
    
    public function get_rows_resgen_nis() {
        
        $page = ($this->input->post('page', TRUE) ? $this->input->post('page', TRUE) : 1);
        $start = isset($_POST['start']) ? $this->db->escape_str($this->input->post('start', TRUE)) : 0;
        $limit = isset($_POST['limit']) ? $this->db->escape_str($this->input->post('limit', TRUE)) : $this->config->item("length_records");
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $search = isset($_POST['query']) ? $this->db->escape_str($this->input->post('query', TRUE)) : '';       
        $result = $this->datamodel->get_rows_resgen_nis($nis,$thajaran,$thbl,$search, $start, $limit);
        echo $result;
    }
    
    public function get_rows_potongan_resgen(){
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $kdbiaya=isset($_POST['kd_biaya']) ? $this->db->escape_str($this->input->post('kd_biaya', TRUE)) : '';
        $result = $this->datamodel->get_rows_resgenpotongan($nis,$thbl,$kdbiaya);
        echo $result;
        
        
        
    }
    
    public function get_rows_potongan(){
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $kdbiaya=isset($_POST['kd_biaya']) ? $this->db->escape_str($this->input->post('kd_biaya', TRUE)) : '';
        $result = $this->datamodel->get_rows_potongan($nis,$thbl,$kdbiaya);
        echo $result;
        
        
        
    }
    public function proses_generate(){
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : 0;
        $id_user = $this->session->userdata('username');
        //cek thajaran semester
        $sem=$this->datamodel-> callFunction('get_semester',array($thajaran));
        if(count($sem)>0){
            if($opt=='thbl'){
            $result = '';
            $spname = 'gen_piutang_thbl';
            $param = array($thbl, $thajaran, $id_user);
            $result = $this->datamodel->SP_execData($spname, $param, true);
            echo $result;
            }
            if($opt=='nis'){
                $result = '';
                $spname = 'gen_piutang_siswa';
                $param = array($nis,$thbl, $thajaran, $id_user);
                $result = $this->datamodel->SP_execData($spname, $param, true);
                echo $result;
            }
        }else{
            $json = array(
                    "success" => false,
                    "msg" => 'Setting Semester Tidak Aktif'
                );
                $results = json_encode($json);
            echo $results;
        }
        
        
    }

    public function set_params() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
//        $result = $this->datamodel->callFunction('get_exists_thbl',array($thbl));
        $result='';
        $json = array(
                        "success" => true,
                        "msg" => ''
                    );
        
        if($opt=='proses'){
            $result = $this->datamodel->callFunction('get_exists_thbl',array($thbl));
            if(count($result)>0){
                if($result[0]->retval==1){
                    $json = array(
                        "success" => true,
                        "msg" => 'Data Piutang sudah ada apakah akan diproses ulang?',
                        "msgcode"=>2
                    );
                }
                if($result[0]->retval==2){
                    $json = array(
                        "success" => false,
                        "msg" => 'Maaf Status Piutang Sudah Lunas !!!',
                        "msgcode"=>0
                    );
                }
                if($result[0]->retval==0){
                    $json = array(
                        "success" => true,
                        "msg" => 'jalankan eksekusi',
                        "msgcode"=>0
                    );
                }
            }
            
        }
        echo json_encode($json);
    }
    
    public function set_params_nis() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $thajaran=isset($_POST['id_th_ajaran']) ? $this->db->escape_str($this->input->post('id_th_ajaran', TRUE)) : '';
        $thbl=isset($_POST['thbl']) ? $this->db->escape_str($this->input->post('thbl', TRUE)) : 0;
        
        $nis=isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : 0;
        $result='';
        $json = array(
                        "success" => true,
                        "msg" => ''
                    );
        
        if($opt=='proses'){
            $result = $this->datamodel->callFunction('get_exists_thbl_nis',array($nis,$thbl));
            if(count($result)>0){
                if($result[0]->retval==1){
                    $json = array(
                        "success" => true,
                        "msg" => 'Data Piutang sudah ada apakah akan diproses ulang?',
                        "msgcode"=>2
                    );
                }
                if($result[0]->retval==2){
                    $json = array(
                        "success" => false,
                        "msg" => 'Maaf Status Piutang Sudah Lunas !!!',
                        "msgcode"=>0
                    );
                }
                if($result[0]->retval==0){
                    $json = array(
                        "success" => true,
                        "msg" => 'jalankan eksekusi',
                        "msgcode"=>0
                    );
                }
            }
            
        }
        echo json_encode($json);
    }
    
    public function get_rows_all() {

        $result = $this->datamodel->get_rows_all();
        echo $result;
    }

    public function get_rows_combo() {

        $result = $this->datamodel->get_rows_combo();
        echo $result;
    }

    public function get_rows_entry() {
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $th = isset($_POST['idth']) ? $this->db->escape_str($this->input->post('idth', TRUE)) : '';
        $result = $this->datamodel->get_rows_entry($nis, $th);
        echo $result;
    }

    public function update_rows() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $nis = isset($_POST['nis']) ? $this->db->escape_str($this->input->post('nis', TRUE)) : '';
        $th_ajaran_mulai = isset($_POST['th_ajaran_mulai']) ? $this->db->escape_str($this->input->post('th_ajaran_mulai', TRUE)) : '';
        $kd_biaya = isset($_POST['kd_biaya']) ? $this->db->escape_str($this->input->post('kd_biaya', TRUE)) : '';
        $potongan = isset($_POST['mpotongan']) ? $this->db->escape_str($this->input->post('mpotongan', TRUE)) : 0;
        $thbl_mulai = isset($_POST['thbl_mulai']) ? $this->db->escape_str($this->input->post('thbl_mulai', TRUE)) : NULL;
        $thbl_selesai = isset($_POST['thbl_selesai']) ? $this->db->escape_str($this->input->post('thbl_selesai', TRUE)) : NULL;
        $id_potongan = isset($_POST['id_potongan']) ? $this->db->escape_str($this->input->post('id_potongan', TRUE)) : NULL;
        $keterangan = isset($_POST['keterangan']) ? $this->db->escape_str($this->input->post('keterangan', TRUE)) : NULL;
        $id_user = $this->session->userdata('username');

        $result = '';
        $spname = 'sp_potongan';
        $param = array($opt, $id_potongan, $nis, $th_ajaran_mulai, $thbl_mulai, $thbl_selesai, $kd_biaya, $potongan, $keterangan, $id_user);
        $result = $this->datamodel->SP_execData($spname, $param, true);

        echo $result;
    }

    public function delete_row() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $data = isset($_POST['postdata']) ? json_decode($this->input->post('postdata', TRUE)) : array();
        $id_potongan = $data->id_potongan;
        $nis = $data->nis;
        $th_ajaran_mulai = $data->th_ajaran_mulai;
        $thbl_mulai = $data->thbl_mulai;
        $thbl_selesai = $data->thbl_selesai;
        $kd_biaya = $data->kd_biaya;
        $potongan = $data->potongan;
        $keterangan = $data->keterangan;
        $tgl_entri = $data->tgl_entri;
        $id_user = $data->id_user;



        $spname = 'sp_potongan';
        $param = array($opt, $id_potongan, $nis, $th_ajaran_mulai, $thbl_mulai, $thbl_selesai, $kd_biaya, $potongan, $keterangan, $id_user);
        $result = $this->datamodel->SP_execData($spname, $param, true);
        echo $result;
    }

}

?>
